package cn.zhangtianci.simple_demo.dubbo.provider;

import javax.annotation.Resource;

import com.alibaba.dubbo.config.annotation.Service;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.http.HttpStatus;
import cn.zhangtianci.simple_demo.dubbo.api.ApiService;
import org.springframework.beans.factory.annotation.Value;
import cn.zhangtiancinb.simple_demo.exception_switch.client.ExceptionSwitchUtil;

/**
 * 接口调用服务实现
 *
 * @author 张天赐
 */
@Service
public class ApiServiceImpl implements ApiService {
    @Value("${number:3}")
    Integer number;
    @Value("${dubbo.application.name}")
    String dubboApplicationName;
    @Resource
    ExceptionSwitchConfigProperties exceptionSwitchConfig;

    /**
     * 调用
     *
     * @param value 入参
     * @return 返回值
     */
    @Override
    public String call(int value) {
        ExceptionSwitchUtil.getInstance(BeanUtil.beanToMap(exceptionSwitchConfig)).callQuickly("应用[simple_demo.dubbo_provider]服务[Api]方法[call]");
        // 返回200
        if (value % number != 0) {return dubboApplicationName + HttpStatus.OK; }
        // 抛出异常
        else {throw new RuntimeException(dubboApplicationName + HttpStatus.INTERNAL_SERVER_ERROR);}
    }
}
